﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IOEntity;
using System.Data.SqlClient;
using System.Data;
namespace IODataAccess
{
   public class clsDADanhMuc
    {

       public DataTable GetDataTableSQL(string sql,string tbName) {
           DataTable tb = new DataTable(tbName);
           clsDAConnection conn = new clsDAConnection();
           conn.openCon();
           SqlDataAdapter apdapter = new SqlDataAdapter(sql,conn.getCon());
           apdapter.Fill(tb);
           conn.closeCon();
           return tb;
       }

      
       public bool InsertDanhMuc(clsDanhMuc dm) {
           string sql = "Insert into tblDanhMuc(TenDanhMuc,TypeID,MoTa) values(@TenDM,@Type,@Mota)";
           bool st=false;
           clsDAConnection conn=new clsDAConnection();
           conn.openCon();
           using (SqlCommand com = conn.getCon().CreateCommand()) {
               com.CommandType = CommandType.Text;
               com.CommandText = sql;

               // Create a SqlParameter object for the title parameter.
               SqlParameter p1 = com.CreateParameter();
               p1.ParameterName = "@TenDM";
               p1.SqlDbType = SqlDbType.NVarChar;
               p1.Size = 50;
               p1.Value = dm.TenDanhMuc;
               com.Parameters.Add(p1);

               // Use a shorthand syntax to add the id parameter.
               com.Parameters.Add("@Type", SqlDbType.Int).Value = dm.MyType;
               com.Parameters.Add("@Mota", SqlDbType.NVarChar,1000).Value = dm.MoTa;
               // Execute the command and process the result.
               if (com.ExecuteNonQuery()==1)
               {
                   st = true;
               }
               conn.closeCon();
               return st;
           }
       }

       // Sua danh muc
       public bool EditDanhMuc(clsDanhMuc dm)
       {
           string sql = "Update tblDanhmuc set TenDanhmuc=@TenDM,Mota=@Mota Where ID=@ID";
           bool st = false;
           clsDAConnection conn = new clsDAConnection();
           conn.openCon();
           using (SqlCommand com = conn.getCon().CreateCommand())
           {
               com.CommandType = CommandType.Text;
               com.CommandText = sql;

               // Create a SqlParameter object for the title parameter.
               SqlParameter p1 = com.CreateParameter();
               p1.ParameterName = "@TenDM";
               p1.SqlDbType = SqlDbType.NVarChar;
               p1.Size = 50;
               p1.Value = dm.TenDanhMuc;
               com.Parameters.Add(p1);

               // Use a shorthand syntax to add the id parameter.
               //com.Parameters.Add("@Type", SqlDbType.Int).Value = dm.MyType;
               com.Parameters.Add("@Mota", SqlDbType.NVarChar, 1000).Value = dm.MoTa;
               com.Parameters.Add("@ID", SqlDbType.Int).Value = dm.Id;
               // Execute the command and process the result.
               if (com.ExecuteNonQuery() == 1)
               {
                   st = true;
               }
               conn.closeCon();
               return st;
           }
       }

       public bool DeleteDM(clsDanhMuc dm)
       {
           string sql = "Delete from tblDanhMuc Where ID=@ID";
           bool st = false;
           clsDAConnection conn = new clsDAConnection();
           conn.openCon();
           using (SqlCommand com = conn.getCon().CreateCommand())
           {
               com.CommandType = CommandType.Text;
               com.CommandText = sql;
               com.Parameters.Add("@ID", SqlDbType.Int).Value = dm.Id;
               // Execute the command and process the result.
               if (com.ExecuteNonQuery() == 1)
               {
                   st = true;
               }
               conn.closeCon();
               return st;
           }
       }
    }
}
